﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TsCore;
using System.Web.Security;
using TsWeb.Model;

namespace TsWeb.Followers
{
    public partial class FollowersHome : System.Web.UI.Page
    {

        

        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static string GetTooltipInfo(string id, string SportId, string day, string year, string month)
        {
            string result = "";
            try
            {
                int intDay = Int32.Parse(day) - 1; //Esto es porque las columnas comienzan en 0
                DateTime matchDate = new DateTime(Int32.Parse(year), Int32.Parse(month), intDay);
                List<Match> MatchesInDate = TsSystem.Instance.GetMatchesForDateAndSport(matchDate, Int32.Parse(SportId));
              
                foreach (Match m in MatchesInDate)
                {
                    result += m.Date + " " + m.HomeTeam.Name + " vs " + m.VisitorTeam.Name + "\n";
                }
              
            }
            catch (Exception ex)
            {
                //TODO
            }

            return result;


        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindUserData(DateTime.Now);
        }

        private void BindUserData(DateTime date)
        {
            TsUser loggedUser = TsSystem.Instance.GetFullUserData(Membership.GetUser().UserName);
            ListViewComments.DataSource = GetCommentsForDisplay(loggedUser.Favorites);
            ListViewComments.DataBind();
            CalendarGrid.DataSource = TsSystem.Instance.GetCalendarForDate(date.Month,date.Year);
            Session["Year"] = date.Year;
            Session["Month"] = date.Month;
            MonthLabel.Text = date.ToString("MMMM");
            
            CalendarGrid.DataBind();
            CalendarGrid.UseAccessibleHeader = true;
            if (CalendarGrid.HeaderRow != null)
            {
                CalendarGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
            }      
            if (CalendarGrid.Columns.Count > 0)
                CalendarGrid.Columns[0].Visible = false;
          
            
            
        }

        private List<CommentModel> GetCommentsForDisplay(List<Team> Favorites)
        {
            List<CommentModel> retList = new List<CommentModel>();
            List<Team> FavoritesOrdered = (from t in Favorites
                                           orderby t.Name
                                           select t).ToList<Team>();
            foreach (Team t in FavoritesOrdered)
            {
                foreach (Comment c in TsSystem.Instance.GetCommentsFromTeam(t))
                {
                    CommentModel cmt = new CommentModel();
                    cmt.CmtDate = c.CmtDate;
                    cmt.Name = t.Name;
                    cmt.Logo = t.Image;//.Remove(0, 9);
                    cmt.Text = c.Text;
                    retList.Add(cmt);
                    
                }
            }
            return retList;
        }

        protected void CalendarGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                
                string year = Session["Year"].ToString();
                string month = Session["Month"].ToString();
                for (int i = 2; i < e.Row.Cells.Count; i++)
                {
                    TableCell c = e.Row.Cells[i];
                    if (c.Text != "")
                    {
                        c.Attributes.Add("data-toggle",
                        "tooltip");
                        c.Attributes.Add("data-html",
                        "true");
                        c.Attributes.Add("title",
                       "");

      
                    
                        

                        c.Attributes.Add("id", c.UniqueID);
                        c.Attributes.Add("onmouseover",
                        "GetMatchesData('" + c.UniqueID.ToString() + "','" + e.Row.Cells[0].Text + "','" + i.ToString() + "','" + year + "','" + month + "');");
                    
                    }

                }
                
                
               
            }
        }

        protected void NextBtn_Click(object sender, EventArgs e)
        {
            int month = Int32.Parse(Session["Month"].ToString());
            int year = Int32.Parse(Session["Year"].ToString());
            DateTime matchDate = new DateTime(year,month,1);
            matchDate = matchDate.AddMonths(1);
            BindUserData(matchDate);


        }

        protected void PreBtn_Click(object sender, EventArgs e)
        {
            int month = Int32.Parse(Session["Month"].ToString());
            int year = Int32.Parse(Session["Year"].ToString());
            DateTime matchDate = new DateTime(year, month, 1);
            matchDate = matchDate.AddMonths(-1);
            BindUserData(matchDate);
        }

       

    }
}